import streamlit as st
from tools.verification import *
from time import sleep
from tools.DBUtil import DBUtil
import pandas as pd

@st.dialog("新增配置")
def create_vm_platform():
    servername = st.text_input("名称")
    serverip = st.text_input("IP地址")
    serverusername = st.text_input("用户名")
    serverpassword = st.text_input("密码", type="password")
    serverdatacentername = st.text_input("数据中心名称")
    servermodelfoldername = st.text_input("模板文件夹名称")

    create_vsphere_button = st.button("创建",key="create_vsphere_button")
    create_vsphere_message = st.empty()

    if create_vsphere_button:
        if servername and serverip and serverusername and serverpassword and serverdatacentername and servermodelfoldername:
            if is_valid_vsphere_exist(servername):
                create_vsphere_message.error("云虚拟机平台已存在,请更换名称")
                return
            # 写入数据库
            DBUtil.uid_db("INSERT INTO config_vsphere (servername, serverip, username, passwd, datacentername, modelfoldername) VALUES (%s, %s, %s, %s, %s, %s)",(servername, serverip, serverusername, serverpassword, serverdatacentername, servermodelfoldername))
            create_vsphere_message.success("新增云虚拟机平台配置成功")
            sleep(1)
            st.rerun()
        else:
            create_vsphere_message.error("请填写完整信息")

@st.dialog("删除配置")
def delete_vm_platform():
    server_options = [""]
    server_result = DBUtil.select_all("SELECT servername FROM config_vsphere;")
    if len(server_result) != 0:
        server_options = [server['servername'] for server in server_result]
    result = st.multiselect("选择云虚拟机平台", server_options, placeholder="")
    delete_vsphere_button = st.button("删除",key="delete_vsphere_button")
    if delete_vsphere_button:
        if result:
            for server in result:
                DBUtil.uid_db("DELETE FROM config_vsphere WHERE servername = %s",(server,))
            st.success("删除云虚拟机平台配置成功!")
            sleep(1)
            st.rerun()
        else:
            st.warning("请选择云虚拟机平台")

@st.dialog("修改配置")
def modify_vm_platform():
    server_options = [""]
    server_result = DBUtil.select_all("SELECT servername FROM config_vsphere;")
    if len(server_result) != 0:
        server_options = server_options + [server['servername'] for server in server_result]

    servername = st.selectbox("选择云虚拟机平台", server_options, index=0, key="modify_vsphere_select")
    if servername:
        # 读取数据库信息
        server_result = DBUtil.select_one("SELECT * FROM config_vsphere WHERE servername = %s",(servername,))
        serverip = server_result['serverip']
        serverusername = server_result['username']
        serverpassword = server_result['passwd']
        serverdatacentername = server_result['datacentername']
        servermodelfoldername = server_result['modelfoldername']

        # 显示表单
        new_servername = st.text_input("更新云平台名称",servername, key="modify_vsphere_name")
        serverip = st.text_input("IP地址",serverip, key="modify_vsphere_ip")
        serverusername = st.text_input("用户名",serverusername, key="modify_vsphere_username")
        serverpassword = st.text_input("密码",serverpassword, type="password", key="modify_vsphere_password")
        serverdatacentername = st.text_input("数据中心名称",serverdatacentername, key="modify_vsphere_datacentername")
        servermodelfoldername = st.text_input("模板文件夹名称",servermodelfoldername, key="modify_vsphere_modelfoldername")

        modify_vsphere_button = st.button("修改",key="modify_vsphere_button")
        modify_vsphere_message = st.empty()

        if modify_vsphere_button:
            if new_servername and serverip and serverusername and serverpassword and serverdatacentername and servermodelfoldername:
                # 更新数据库
                DBUtil.uid_db("UPDATE config_vsphere SET servername=%s, serverip=%s, username=%s, passwd=%s, datacentername=%s, modelfoldername=%s WHERE servername=%s",(new_servername,serverip, serverusername, serverpassword, serverdatacentername, servermodelfoldername, servername))
                modify_vsphere_message.success("修改云虚拟机平台配置成功")
                sleep(1)
                st.rerun()
            else:
                modify_vsphere_message.error("请填写完整信息")
    else:
        st.warning("请选择云虚拟机平台")

# ==================================================页面内容控制========================================================
st.title("云虚拟机平台配置管理")
col1,col2,col3,col4 = st.columns([1,1,1,15])

if col1.button("新增", key="create_vsphere"):
    create_vm_platform()  # 调用创建云虚拟机平台函数

if col2.button("删除",key="delete_vsphere"):
    delete_vm_platform()  # 调用删除云虚拟机平台函数

if col3.button("修改",key="modify_vsphere"):
    modify_vm_platform()  # 调用修改云虚拟机平台函数

data = DBUtil.select_all("SELECT * FROM config_vsphere;")
if len(data) == 0:
    st.warning("暂无数据")
else:
    df = pd.DataFrame(data)
    df.columns = ['序号','云虚拟机平台名称', 'IP地址', '用户名', '密码', '数据中心名称', '模板文件夹名称']
    df.set_index('序号', inplace=True)
    st.dataframe(df)  # 显示虚拟机列表